/**
 * Project Name:mauto-web
 * File Name:AreaService.java
 * Package Name:com.inftech.mauto.sys.service
 * Date:2016年10月7日下午9:14:13
 * Copyright (c) 2016, inf-technology Co., Ltd. All Rights Reserved.
 *
*/

package com.inftech.mauto.core.service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.inftech.mauto.common.utils.CommonUtils;
import com.inftech.mauto.core.dao.ICarDao;
import com.inftech.mauto.core.entity.Car;
import com.mingsoft.base.biz.impl.BaseBizImpl;
import com.mingsoft.base.dao.IBaseDao;

/**
 * 
 * ClassName: CarService <br/>
 * Function: 车辆service. <br/>
 * date: 2016年11月6日 下午10:24:00 <br/>
 *
 * @author puyf
 * @version
 * @since JDK 1.7
 */
@SuppressWarnings({ "unchecked", "rawtypes" })
@Service("carService")
public class CarService extends BaseBizImpl {

	/**
	 * 车辆持久化层注入
	 */
	@Autowired
	private ICarDao carDao;

	@Override
	protected IBaseDao getDao() {
		return carDao;
	}

	/**
	 * 后台服务器地址
	 */
	@Value("${adminUrl}")
	private String adminUrl;

	/**
	 * 
	 * saveOrUpdateCar:新增或更新车辆. <br/>
	 *
	 * @author puyf
	 * @param car
	 * @return
	 * @throws Exception
	 * @since JDK 1.7
	 */
	public void saveOrUpdateCar(Car car) throws Exception {
		// 通过用户id查询用户车辆信息
		int peopleId = car.getPeopleId();
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("people_id", peopleId);
		paramMap.put("del_flag", 0);
		Car carDB = carDao.getEntity(paramMap);
		Date now = CommonUtils.getNowD();
		if (carDB == null) {
			// 用户未绑定车辆，进行车辆绑定
			car.setCreateBy(peopleId);
			car.setCreateDate(now);
			car.setUpdateDate(now);
			car.setCarAddTime(now);
			car.setDelFlag(0);
			carDao.saveEntity(car);
		} else {
			// 用户已绑定车辆，进行车辆更新
			car.setId(carDB.getId());
			car.setUpdateBy(car.getPeopleId());
			car.setUpdateDate(now);
			carDao.updateEntity(car);
		}
	}

	/**
	 * 
	 * queryCar:查询车辆实体. <br/>
	 *
	 * @author puyf
	 * @param id
	 * @return
	 * @throws Exception
	 * @since JDK 1.7
	 */
	public Map<String, Object> queryCar(int peopleId) throws Exception {
		// 通过用户id查询用户车辆信息
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("people_id", peopleId);
		paramMap.put("del_flag", 0);
		Car car = carDao.getEntity(paramMap);
		Map<String, Object> resultMap = new HashMap<String, Object>();
		// 返回对象
		if (car != null) {
			// 车牌号
			resultMap.put("carPlateNumber", car.getCarPlateNumber());
			// 品牌
			resultMap.put("carBrand", car.getCarBrand());
			// 品牌图片
			if (!CommonUtils.isBlank(car.getCarBrandImage())) {
				resultMap.put("carBrandImage", adminUrl + car.getCarBrandImage());
			}
			// 型号
			resultMap.put("carModel", car.getCarModel());
			// 购车价格
			resultMap.put("carBuyPrice", car.getCarBuyPrice());
			// 购买年月
			resultMap.put("carBuyDate", car.getCarBuyDate());
			// 行驶里程
			resultMap.put("carMileage", car.getCarMileage());
		}
		return resultMap;
	}

}
